package com.slt.android.customview.surfaceview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by dp on 2018/1/16.
 */

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback , Runnable{

    private SurfaceHolder surfaceHolder;
    private Canvas mCanvas;
    //子线程标志位
    private boolean mIsDrawing;
    //drawSinCosin
    float x , y;
    Path mPath;
    Paint mPaint;


    public MySurfaceView(Context context) {
        super(context);
        init(context);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3);
        mPaint.setAntiAlias(true);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mIsDrawing = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mIsDrawing = false;
    }

    @Override
    public void run() {
//        drawSinCosin();
        drawMoveLine();
    }
//------------------------------------draw moveLin--- start ---------
    private void drawMoveLine() {

        long start = System.currentTimeMillis();
        while (mIsDrawing){
            drawMoveLine2();
            
            long end = System.currentTimeMillis();
            if(end - start < 100){
                try {
                    Thread.sleep(100 - (end - start));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void drawMoveLine2() {
        try {
            mCanvas = surfaceHolder.lockCanvas();
            mCanvas.drawColor(Color.WHITE);
            mCanvas.drawPath(mPath , mPaint);
        }catch (Exception e){

        }finally {
            if(mCanvas != null){
                surfaceHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(x , y);
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(x , y);
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }
    //------------------------------------draw moveLin--- end ---------


//------------------------------------draw sin--- start ---------
    private void drawSinCosin() {
        while (mIsDrawing){
            drawSinCosin2();
            x += 1;
            y = (float) (100*Math.sin(x * 2 * Math.PI / 180) + 400);
            mPath.lineTo(x , y);
//            if(x == 1000){
//                mIsDrawing = false;
//            }
        }
    }

    private void drawSinCosin2() {
        try {
            mCanvas = surfaceHolder.lockCanvas();
            mCanvas.drawColor(Color.WHITE);
            mCanvas.drawPath(mPath , mPaint);
        }catch (Exception e){

        }finally {
            if(mCanvas != null){
                surfaceHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }
//------------------------------------draw sin--- end ---------
}
